﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using FileHelper;

public partial class Files_AddFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {            
            int folderid = -1;
            int fileid = -1;
            string owner = string.Empty;

            if (Request["folderid"] != null)
            {
                folderid = Convert.ToInt32(Request["folderid"]);
            }

            if (Request["fileid"] != null)
            {
                fileid = Convert.ToInt32(Request["fileid"]);
            }

            if (Request["owner"] != null)
            {
                owner = Request["owner"];
            }            
            
            hdFolderId.Value = folderid.ToString();
            if (folderid > 0)
            {
                FolderInfo objFolder = FileStreamManager.GetFolder(folderid);
                this.lblFolderPath.Text = "Root\\" + objFolder.Path;
            }

            txtOwner.Text = owner;
            if (fileid <= 0)
            {
                this.btnUploading.CommandName = "CMDADD";                
            }
            else
            {
                this.btnUploading.CommandName = "CMDEDIT";
                this.btnUploading.CommandArgument = fileid.ToString();
            }
        }
    }
    protected void btnUploading_Click(object sender, EventArgs e)
    {        
        RemoteFileInfo uploadRequestInfo = new RemoteFileInfo();        
        uploadRequestInfo.FileName = fu.FileName;
        if (!string.IsNullOrEmpty(txtFriendly.Text))
        {
            uploadRequestInfo.FileName = txtFriendly.Text;
        }
        uploadRequestInfo.Length = fu.PostedFile.ContentLength;
        uploadRequestInfo.FileByteStream = fu.PostedFile.InputStream;
        uploadRequestInfo.Owner = txtOwner.Text;        
        if (!string.IsNullOrEmpty(this.hdFolderId.Value))
        {
            uploadRequestInfo.FolderId = Convert.ToInt32(hdFolderId.Value);
        }
        else
        {
            uploadRequestInfo.FolderId = -1;
        }
        FileStreamManager.UploadFile(uploadRequestInfo);
        
    }
}